package tongcheng.com.tongcheng.ui.yemian.shenfen.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yanzhenjie.nohttp.RequestMethod;

import butterknife.Bind;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.base.BaseActivity;
import tongcheng.com.tongcheng.bean.StatusBean;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.network.MyRequest;
import tongcheng.com.tongcheng.network.OnCallBack;
import tongcheng.com.tongcheng.utils.GsonUtil;
import tongcheng.com.tongcheng.utils.Setting;
import tongcheng.com.tongcheng.utils.ShowToast;
import tongcheng.com.tongcheng.utils.TimeCount;

public class YinHangCardActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.zhuce_send)
    TextView mZhuceSend;
    @Bind(R.id.yinhang_ll1)
    LinearLayout mYinhangLl1;
    @Bind(R.id.yinhang_ll2)
    LinearLayout mYinhangLl2;
    @Bind(R.id.yinhang_next)
    Button mYinhangNext;
    @Bind(R.id.title_bar)
    RelativeLayout mBg;
    @Bind(R.id.back_text)
    TextView mBackText;
    @Bind(R.id.yinhang_name)
    EditText mYinhangName;
    @Bind(R.id.yinhang_cardno)
    EditText mYinhangCardno;
    @Bind(R.id.yinhang_kahao)
    EditText mYinhangKahao;
    @Bind(R.id.yinhang_phone)
    EditText mYinhangPhone;
    @Bind(R.id.yinhang_code)
    EditText mYinhangCode;
    private TimeCount time;

   /* @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yin_hang_card);
        ButterKnife.bind(this);
    }*/

    @Override
    public Integer initResid() {
        return R.layout.activity_yin_hang_card;
    }

    @Override
    public void initCreate(Bundle savedInstanceState) {
        settitlebar(mBg);
        time = new TimeCount(60000, 1000,mZhuceSend);
        mTitle.setText("添加银行卡");
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mYinhangNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = mYinhangName.getText().toString().trim();
                String shenfen = mYinhangCardno.getText().toString().trim();
                String kahao = mYinhangKahao.getText().toString().trim();
                String phone = mYinhangPhone.getText().toString().trim();
                String code = mYinhangCode.getText().toString().trim();
              /*  if (TextUtils.isEmpty(name)) {
                    ShowToast.showToast("请输入持卡人姓名");
                } else if (TextUtils.isEmpty(shenfen)) {
                    ShowToast.showToast("请输入身份证号码");
                }else if (TextUtils.isEmpty(kahao)) {
                    ShowToast.showToast("请输入银行卡号");
                }else if (TextUtils.isEmpty(phone)) {
                    ShowToast.showToast("请输入手机号");
                } else if (TextUtils.isEmpty(code)) {
                    ShowToast.showToast("请输入验证码");
                } else {
                    UpInfo();
                }*/
                startactivity(YinHangCardActivity.this, BaoZhenJinActivity.class);
                //UpInfo();
            }
        });

        //发送验证码
        mZhuceSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String phone = mYinhangPhone.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ShowToast.showToast("手机号码不能为空");
                } else {
                    time.start();
                    sendCode();
                }

            }
        });



    }
    //上传银行卡信息
    private void UpInfo() {
        MyRequest request = new MyRequest(ConStans.YinHang_Url, RequestMethod.POST);
        request.add("type", 2);
        request.add("account_id", Setting.getPhone());
        request.add("name", mYinhangName.getText().toString().trim());
        request.add("phone", mYinhangPhone.getText().toString().trim());
        request.add("code", mYinhangCode.getText().toString().trim());
        request.add("card_no", mYinhangKahao.getText().toString().trim());
        request.add("id_no", mYinhangCardno.getText().toString().trim());
        CallServer.getInstance().addPost(this,request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                StatusBean statusBean = GsonUtil.GsonToBean(json, StatusBean.class);
                if (statusBean != null) {

                    if (statusBean.msg != null) {
                        ShowToast.showToast(statusBean.msg);
                    }


                    if (statusBean.status == 1) {
                        startactivity(YinHangCardActivity.this, BaoZhenJinActivity.class);
                    }

                }
            }
        });
    }

    //发送验证码
    private void sendCode() {
        MyRequest request = new MyRequest(ConStans.YinHang_Url, RequestMethod.POST);
        request.add("type", 1);
        request.add("phone", mYinhangPhone.getText().toString().trim());
        CallServer.getInstance().addPost(this,request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                StatusBean statusBean = GsonUtil.GsonToBean(json, StatusBean.class);
                if (statusBean != null) {

                    if (statusBean.msg != null) {
                        ShowToast.showToast(statusBean.msg);
                    }

                }
            }
        });
    }


}
